package com.ky.tms.controller;


import com.ky.tms.entity.TmsCustomer;
import com.ky.tms.service.ITmsCustomerService;
import com.ky.tms.util.CommonResult;
import com.ky.tms.util.JwtUtil;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author chengkaimao
 * @since 2020-03-21
 */
@RestController
@RequestMapping("/tms/tms-customer")
public class TmsCustomerController {

    @Autowired
    private ITmsCustomerService tmsCustomerService;

    @ApiOperation("客户表格分页数据")
    @GetMapping("page")
    public CommonResult page(TmsCustomer tmsCustomer, @RequestParam Long pageSize, @RequestParam Long current, HttpServletRequest request){
        String userName = JwtUtil.getUserName(request);
        return CommonResult.buildSuccess(tmsCustomerService.page(userName, tmsCustomer, pageSize, current));
    }

    @ApiOperation("新增客户")
    @PostMapping
    public CommonResult add(@RequestBody TmsCustomer tmsCustomer, HttpServletRequest request){
        String userName = JwtUtil.getUserName(request);
        tmsCustomerService.add(userName, tmsCustomer);
        return CommonResult.buildSuccess(null);
    }

    @ApiOperation("修改客户")
    @PutMapping
    public CommonResult update(@RequestBody TmsCustomer tmsCustomer, HttpServletRequest request){
        String userName = JwtUtil.getUserName(request);
        tmsCustomerService.update(userName,tmsCustomer);
        return CommonResult.buildSuccess(null);
    }

    @ApiOperation("删除客户")
    @DeleteMapping
    public CommonResult del(@RequestBody List<Long> ids, HttpServletRequest request){
        String userName = JwtUtil.getUserName(request);
        tmsCustomerService.del(userName, ids);
        return CommonResult.buildSuccess(null);
    }

    @ApiOperation("搜索下拉数据")
    @GetMapping("select-list")
    public CommonResult selectList(HttpServletRequest request) {
        String userName = JwtUtil.getUserName(request);
        return CommonResult.buildSuccess(tmsCustomerService.selectList(userName));
    }

}
